package io.renren.modules.lanling.service.impl;

import cn.hutool.core.lang.Validator;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.lanling.dao.SchoolPicDao;
import io.renren.modules.lanling.entity.SchoolPicEntity;
import io.renren.modules.lanling.service.SchoolPicService;


@Service("schoolPicService")
public class SchoolPicServiceImpl extends ServiceImpl<SchoolPicDao, SchoolPicEntity> implements SchoolPicService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        EntityWrapper<SchoolPicEntity> entityWrapper = new EntityWrapper<>();

        Object name = params.get("name");
        if (Validator.isNotEmpty(name)) {
            entityWrapper.like("name", name.toString());
        }

        Page<SchoolPicEntity> page = this.selectPage(
                new Query<SchoolPicEntity>(params).getPage(), entityWrapper);

        return new PageUtils(page);
    }

    @Override
    public void deleteBySchoolId(Integer schoolId) {
        this.baseMapper.deleteBySchoolId(schoolId);
    }

    @Override
    public void saveSchoolImgList(Integer schoolId, List<SchoolPicEntity> schoolImgList) {
        if (schoolImgList.size() > 0) {
            int i = 0;
            for (SchoolPicEntity schoolPicEntity : schoolImgList) {
                schoolPicEntity.setSchoolId(schoolId);
                schoolPicEntity.setCreateDate(new Date());
                schoolPicEntity.setSortOrder(i);
                this.baseMapper.insert(schoolPicEntity);
                i++;
            }
        }
    }

}
